<?php
namespace Admin\Controller;

class ClassroomController extends AdminController {
	public function index(){
		//定义搜索项
		//通用的搜索项定义无法使用，因为学校选择后会要求下级建筑是关联
		$this->assign('schoolList', R('Common/getSchoolList'));
		$map = array();
		$searchFields = array('school_id', 'building_id');
		foreach ($searchFields as $field){
			if (I($field) > 0) {
				$map[$field] = I($field);
			}
		}
		$list   = $this->lists('Classroom', $map);
		$this->assign('_list', $list);
		$this->meta_title = '教室列表';
		$this->display();
	}
	
	Public function edit(){
		$id = I('get.id');
		$info = D('Classroom')->where(array('id' => $id))->find();
		$content = unserialize($info['content']);
		$this->assign('info', $info);
		$this->assign('schoolList', R('Common/getSchoolList'));
		$buildingList = D('Building')->where(array('school_id' => $info['school_id']))->select();
		$this->assign('buildingList', $buildingList);
		$this->meta_title = '修改教室';
		$this->display();
	}
	
	public function addBatch(){
		$this->assign('schoolList', R('Common/getSchoolList'));
		$this->meta_title = '添加教室(批量)';
		$this->display();
	}
	
	public function addOne(){
		$this->assign('schoolList', R('Common/getSchoolList'));
		$this->meta_title = '添加教室(单独)';
		$this->display();
	}
	
	public function doAddOne(){
		if (IS_POST) {
			$schoolId 	= I('post.school_id');
			$buildingId = I('post.building_id');
			$title 		= I('post.title');
			$coordinate = I('post.coordinate');
				
			//判断相同学校同一建筑同一教室信息是否已经存在
			$map = array(
					'school_id' => $schoolId,
					'building_id' => $buildingId,
					'title' => $title
			);
			$count = D('Classroom')->where($map)->count();
			if ($count > 0) {
				$this->error('此教室已存在，不能重复添加', U('index'));
			}
			$data = array();
			$data['title'] 			= $title;
			$data['school_id'] 		= $schoolId;
			$data['building_id'] 	= $buildingId;
			$data['coordinate'] 	= $coordinate;
			$ret = D('Classroom')->add($data);
			if ($ret !== false) {
				$this->success('添加成功', U('index'));
			}else {
				$this->error('出错');
			}
		}
	}
	
	public function doAddBatch(){
		if (IS_POST) {
			$schoolId 	= I('post.school_id');
			$buildingId = I('post.building_id');
			$classrooms = I('post.classrooms');
			
			//判断相同学校同一建筑信息是否已经存在，存在则只能在其它地方一个一个添加
			$map = array(
				'school_id' => $schoolId,
				'building_id' => $buildingId
			);
			$count = D('Classroom')->where($map)->count();
			if ($count > 0) {
				$this->error('此学校的此建筑已经添批量添加过学校，请跳转到单独添加处', U('index'));
			}
			
			//组装数据
			$array = preg_split('/[\r\n]+/', trim($classrooms, ",;\r\n"));
			$data = array();
			foreach ($array as $record){
				$item = array();
				$recordArr = explode('#', $record);
				$item['title'] 		 = trim($recordArr[0]);
				$item['school_id']   = $schoolId;
				$item['building_id'] = $buildingId;
				$item['coordinate']  = trim($recordArr[1]);
				$data[] = $item;
			}
			$ret = D('Classroom')->addAll($data);
			if ($ret === false) {
				$this->success('添加成功', U('index'));
			}else {
				$this->error('出错');
			}
		}
	}
	
	public function gen(){
		$this->assign('cates', R('Common/getQrcodeCates'));
		$this->assign('schoolList', R('Common/getSchoolList'));
		$this->display();
	}
	
    public function doGen(){
    	$map = array();
    	$cateId = I('qrcode_category_id');
    	$schoolId = I('school_id');
    	$title = I('title');
    	if ($cateId > 0) {
    		$map['qrcode_category_id'] = $cateId;
    	}
    	if ($schoolId > 0) {
    		$map['school_id'] = $schoolId;
    	}
    	if (!empty($title)) {
    		$map['title'] = $title;
    	}
    	
    	vendor('phpqrcode'); //$qrcode = new \QRcode();
    	$qrcodeList = D('Qrcode')->field('id,school_id')->where($map)->select();
    	foreach ($qrcodeList as $item){
    		$dirName = './Data/qrcode/' . $item['school_id']; //按学校ID分目录，不需要再按分类ID
    		if (!is_dir($dirName)) {
    			mkdir($dirName);
    		}
    		$filename 	= $dirName . '/' . $item['id'] . '.png';
    		$genTime 	= time();
    		$data 		= $item['id'] . '@' . $genTime; //加密后写入
    		\QRcode::png($data, $filename, QR_ECLEVEL_L, 15, 1);
    		D('Qrcode')->save(array('id' => $item['id'], 'gen_time' => $genTime));
    		/* echo $item['id'] . ' success!<br/>';
    		ob_flush();
    		flush(); */
    	}
    	$this->success('生成成功');
    }
    
    public function reGen(){
    	//
    }
    
	public function del(){
    	$id = I('get.id');
    	$id = intval($id);
    	$ret = D('Classroom')->where(array('id' => $id))->delete();
    	if ($ret !== false) {
    		$this->success('删除成功');
    	}
    }

}